package org.water.base.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.water.base.entity.ResultBaseBean;

/**
 * 抽象类
 * @author qzy
 *
 */
public abstract class BaseController {

	public BaseController() {
		// TODO Auto-generated constructor stub
	}
	//日志
	private static Logger logger = LogManager.getLogger(BaseController.class.getName());
	
	@Autowired
	protected LocalValidatorFactoryBean beanValidator;
	/**
	 * 获取验证器
	 * @return
	 */
	public Validator getValidator()
	{
		return beanValidator.getValidator();
	}
	/**
	 * 返回结果
	 * @param success
	 * @param msg
	 * @return
	 */
	protected  ResultBaseBean getBaseBean(Boolean success,String msg)
	{
		ResultBaseBean bean=new ResultBaseBean();
		bean.setSuccess(success);
		bean.setMsg(msg);
		return bean;
	}
	/**
	 * 获取验证错误信息
	 * @param validResrult
	 * @return
	 */
	protected List<Object> getErrorMsg(Set<ConstraintViolation> validResrult)
	{
		ArrayList<Object> errs=new ArrayList<>();
		Iterator<ConstraintViolation> iterator=validResrult.iterator();
		while(iterator.hasNext())
		{
			ConstraintViolation violation=iterator.next();
			Map<String, String> error=new HashMap<>();
			error.put("field", violation.getPropertyPath().toString());
			error.put("msg", violation.getMessage());
			errs.add(error);
		}
		return errs;
	}
}
